package serializer

import (
	"singo/model"
	"time"
)

// Project 序列化器
type Project struct {
	UID          string      `json:"id"`
	Name         string      `json:"name"`
	Intro        string      `json:"intro"`
	Status       int         `json:"status"`
	Head         interface{} `json:"head"`
	Organization interface{} `json:"organization"`
	DeadlineAt   string      `json:"deadline_at"`
	CreatedAt    string      `json:"created_at"`
}

type ProjectSimple struct {
	UID  string `json:"id"`
	Name string `json:"name"`
}

// BuildProject 序列化
func BuildProject(item model.Project) Project {
	return Project{
		UID:          item.UID,
		Name:         item.Name,
		Intro:        item.Intro,
		Status:       item.Status,
		Head:         BuildUserSimple(item.Head),
		Organization: BuildOrganizationSimple(item.OrganizationUID),
		CreatedAt:    item.CreatedAt.In(time.Local).Format("2006-01-02 15:04:05"),
		DeadlineAt:   serializerTime(item.DeadlineAt),
	}
}

func BuildProjectSimple(projectID string) ProjectSimple {
	if projectID != "" {
		project := model.Project{}
		model.DB.First(&project, "uid = ?", projectID)

		return ProjectSimple{
			UID:  project.UID,
			Name: project.Name,
		}
	} else {
		return ProjectSimple{
			UID:  "",
			Name: "",
		}
	}
}

// BuildProjects 序列化列表
func BuildProjects(items []model.Project) (projects []Project) {
	for _, item := range items {
		project := BuildProject(item)
		projects = append(projects, project)
	}
	return projects
}
